ARG FLINK_VERSION
ARG BASE_OS

FROM registry.openanolis.cn/openanolis/anolisos:23 as downloader
ARG FLINK_VERSION

ARG FLINK_FILE=flink-${FLINK_VERSION}-bin-scala_2.12

RUN yum install -y tar gzip

RUN curl -O -m 600 https://mirrors.ustc.edu.cn/apache/flink/flink-${FLINK_VERSION}/${FLINK_FILE}.tgz
RUN gzip -d ${FLINK_FILE}.tgz
RUN tar xf ${FLINK_FILE}.tar
RUN mv flink-${FLINK_VERSION} flink

RUN set -eux; \
    # Replace default REST/RPC endpoint bind address to use the container's network interface \
    sed -i 's/rest.address: localhost/rest.address: 0.0.0.0/g' flink/conf/flink-conf.yaml; \
    sed -i 's/rest.bind-address: localhost/rest.bind-address: 0.0.0.0/g' flink/conf/flink-conf.yaml; \
    sed -i 's/jobmanager.bind-host: localhost/jobmanager.bind-host: 0.0.0.0/g' flink/conf/flink-conf.yaml; \
    sed -i 's/taskmanager.bind-host: localhost/taskmanager.bind-host: 0.0.0.0/g' flink/conf/flink-conf.yaml; \
    sed -i '/taskmanager.host: localhost/d' flink/conf/flink-conf.yaml

COPY gosu-amd64 /
COPY gosu-arm64 /

RUN set -eux; \
    if [ "$(uname -m)" == "aarch64" ]; then \
        mv gosu-arm64 gosu; \
    else \
        mv gosu-amd64 gosu; \
    fi; \
    chmod +x gosu

FROM ${BASE_OS}

ARG FLINK_VERSION
ARG OS_SUFFIX
ARG JAVA_VERSION
ARG flink_uid=9999

LABEL org.opencontainers.image.title="Flink" \
      org.opencontainers.image.version="${FLINK_VERSION}-${OS_SUFFIX}"

ENV SPARK_VERSION ${SPARK_VERSION}
ENV JAVA_HOME /etc/alternatives/jre
ENV PATH $JAVA_HOME/bin:$PATH
ENV DISABLE_JEMALLOC=true

COPY --from=downloader /gosu /usr/local/bin/gosu

RUN set -eux; \
    gosu nobody true; \
    yum update -y; \
    yum install -y net-tools iputils hostname; \
    case "${JAVA_VERSION}" in \
        "8" ) \
            yum install -y java-1.8.0-alibaba-dragonwell \
    ;; \
        "11" ) \
            yum install -y java-11-alibaba-dragonwell \
    ;; \
        * ) \
            echo unsupported java version: ${JAVA_VERSION}; exit 1 \
    ;; \
    esac; \
    yum clean all; \
    groupadd --system --gid=${flink_uid} flink; \
    useradd --system --uid=${flink_uid} --gid=flink flink

COPY --chown=flink:flink --from=downloader /flink /opt/flink

ENV FLINK_HOME /opt/flink
ENV PATH ${FLINK_HOME}/bin:${PATH}

WORKDIR ${FLINK_HOME}

COPY docker-entrypoint.sh /
RUN chmod 755 /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

USER flink
EXPOSE 6123 8081
CMD ["help"]
